﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class CardBLL
    {
        GaraDataContext DB = new GaraDataContext();

        public IQueryable SelectAll()
        {
            return (from card in DB.Cards
                    join type in DB.Types
                    on card.TypeID equals type.TypeID into result
                    from li in result.DefaultIfEmpty()
                    select new
                    {
                        card.CardID,
                        li.TypeName,
                        card.Status
                    }).Distinct().OrderByDescending(p => p.CardID).AsQueryable();
        }

        public CardDTO LoadInfoCard(string cardID)
        {
            return (from c in DB.Cards
                    join t in DB.Types
                        on c.TypeID equals t.TypeID into r
                    from result in r.DefaultIfEmpty()
                    where c.CardID == cardID
                    select new CardDTO
                    {
                        CardID = c.CardID,
                        TypeName = result.TypeName,
                        FeeRegister = result.FeeRegister,
                        FeeExtension = result.FeeExtension,
                        Status = c.Status
                    }).FirstOrDefault<CardDTO>();
        }

        public string Insert(CardDTO cardDTO)
        {
            try
            {
                Card card = new Card();
                card.CardID = cardDTO.CardID;
                card.TypeID = cardDTO.TypeID;
                card.Status = cardDTO.Status;

                DB.Cards.InsertOnSubmit(card);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Update(CardDTO cardDTO)
        {
            try
            {
                Card card = DB.Cards.Where(s => s.CardID == cardDTO.CardID).FirstOrDefault();
                card.CardID = cardDTO.CardID;
                card.TypeID = cardDTO.TypeID;
                card.Status = cardDTO.Status;

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public Boolean CheckCardIDExist(string cardID)
        {
            int count = DB.Cards.Where(s => s.CardID == cardID).Count();
            if (count > 0) return true;
            else return false;
        }

        public Boolean CheckCard(string cardID)
        {
            int count = (from p in DB.Cards
                         join t in DB.Types
                             on p.TypeID equals t.TypeID into r
                         from li in r.DefaultIfEmpty()
                         where li.Status == "Sử dụng" && p.CardID==cardID
                         select li).Distinct().Count();
            if (count > 0) return true;
            else return false;
        }

        public Boolean CheckCardNotRegister(string cardID)
        {
            int count = (from c in DB.Cards
                         join t in DB.Types
                             on c.TypeID equals t.TypeID into r
                         from result in r.DefaultIfEmpty()
                         where c.CardID == cardID && result.Status == "Sử dụng" && c.Status == "Chưa đăng ký" && (result.TypeName == "Thẻ tháng" || result.TypeName == "Thẻ vip")
                         select result).Distinct().Count();
            if (count > 0) return true;
            else return false;
        }

        public Boolean CheckCardTheLuot(string cardID)
        {
            int count = (from c in DB.Cards
                         join t in DB.Types
                             on c.TypeID equals t.TypeID into r
                         from result in r.DefaultIfEmpty()
                         where c.CardID == cardID && result.TypeName == "Thẻ lượt"
                         select result).Distinct().Count();
            if (count > 0) return true;
            else return false;
        }

        public Boolean CheckCardRegister(string cardID)
        {
            int count = (from c in DB.Cards
                         join t in DB.Types
                             on c.TypeID equals t.TypeID into r
                         from result in r.DefaultIfEmpty()
                         where c.CardID == cardID && result.Status == "Sử dụng" && c.Status != "Chưa đăng ký" && (result.TypeName == "Thẻ tháng" || result.TypeName == "Thẻ vip")
                         join re in DB.Registers
                         on c.CardID equals re.CardID into r2
                         from li in r2.DefaultIfEmpty()
                         select li).Distinct().Count();
            if (count > 0) return true;
            else return false;
        }

        public string UpdateCardStatus(string CardID)
        {
            try
            {
                Card card = DB.Cards.Where(s => s.CardID == CardID && s.Status == "Chưa đăng ký").FirstOrDefault();
                card.Status = "Sử dụng";

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateCardStatusExtension(string CardID)
        {
            try
            {
                Card card = DB.Cards.Where(s => s.CardID == CardID && (s.Status == "Hết hạn" || s.Status == "Sử dụng")).FirstOrDefault();
                card.Status = "Sử dụng";

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }




    }
}
